$(document).ready(function(){
    //Sort
    $('.sym_video_admin ul').sortable({
        handle: '.sort',
        placeholder: 'ui-state-highlight',
        update: function(){
            var sort=new Array();
            $('.sym_video_admin ul li div.id').each(function(){
                sort.push($(this).html());
            });
            $.ajax({
                data:{
                    'symbiont': 'video.db_sort',
                    'category_id': $('.sym_video_category_id').text(),
                    'sort': sort
                },
                success:function(r){
                    
                },
                dataType: 'html'
            });
        }
    });
    //Add
    $('.sym_video_admin .symbiosis-button-add').click(function(){
        if($('#sym_video_add').length){
            $('#sym_video_add').dialog('open');
        }
        else{
            $('#windows').append('<div id="sym_video_add" title="Video add"></div>');
            $('#sym_video_add').dialog({
                modal: true,
                width: 380,
                open: function(event, ui) {
                    $.ajax({
                        data:{
                            symbiont: 'video.add'
                        },
                        success:function(r){
                            $('#sym_video_add').html(r);
                            $('.sym_video_add .tabs')
                            .tabs();
                            
                        },
                        dataType: 'html'
                    });
                },
                beforeclose: function(event, ui) {
                    
                },
                close: function(event, ui) {
                    
                },
                buttons:{
                    "Cancel":function(){
                        $(this).dialog("close");
                    },
                    "Ok":function(){
                        t=this;
                        title={};
                        description={};
                        name=$(t).find('.name').val();
                        parnet=$(t).find('.parent').val();
                        category_id=$('.sym_video_category_id').text();
                        $(t).find('.title').each(function(){
                            title[$(this).attr('title')]=$(this).val();
                        });
                        $(t).find('.description').each(function(){
                            description[$(this).attr('title')]=$(this).val();
                        });
                        if($('#image').val()&&$('#upvideo').val()){
                            var image;
                            $.ajaxFileUpload({
                                url:'?page=$&upload&name=image&dir=uploads/images/',
                                secureuri:false,
                                fileElementId:'image',
                                dataType: 'json',
                                success: function (data, status){
                                    if(typeof(data.error) != 'undefined'){
                                        if(data.error != ''){
                                            alert(data.error);
                                        }
                                        else{
                                            image=data.name;
                                            $.ajaxFileUpload({
                                                url:'?page=$&upload&name=video&dir=uploads/videos/',
                                                secureuri:false,
                                                fileElementId:'upvideo',
                                                dataType: 'json',
                                                success: function (data, status){
                                                    if(typeof(data.error) != 'undefined'){
                                                        if(data.error != ''){
                                                            alert(data.error);
                                                        }
                                                        else{
                                                            $.ajax({
                                                                data:{
                                                                    'symbiont': 'video.db_add',
                                                                    'cover': 'uploads/images/'+image,
                                                                    'video': 'uploads/videos/'+data.name,
                                                                    'title': title,
                                                                    'description': description,
                                                                    'name': name,
                                                                    'parent': parent,
                                                                    'category_id': category_id
                                                                },
                                                                success:function(r){
                                                                    $(t).dialog("close");
                                                                    window.location.reload();
                                                                },
                                                                dataType: 'html'
                                                            });
                                                        }
                                                    }
                                                },
                                                error: function (data, status, e){
                                                    alert(e);
                                                }
                                            });
                                        }
                                    }
                                },
                                error: function (data, status, e){
                                    alert(e);
                                }
                            
                            });
                        }
                        else{
                            $.ajax({
                                data:{
                                    'symbiont': 'video.db_add',
                                    'cover': '',
                                    'video': '',
                                    'title': title,
                                    'description': description,
                                    'name': $(t).find('.name').val(),
                                    'parent': $(t).find('.parent').val(),
                                    'category_id': $('.sym_video_category_id').text()
                                },
                                success:function(r){
                                    $(t).dialog("close");
                                    window.location.reload();
                                },
                                dataType: 'html'
                            });
                        }
                    }
                }
            });
        }
    });
    //Edit
    $('.sym_video_admin .edit').click(function(){
        t=this;
        id=($(t).parent().parent().find('.id').first().text());
        if($('#sym_video_edit').length){
            $('#sym_video_edit').dialog('open');
        }
        else{
            $('#windows').append('<div id="sym_video_edit" title="video edit"></div>');
            $('#sym_video_edit').dialog({
                modal: true,
                width: 380,
                open: function(event, ui) {
                    $.ajax({
                        data:{
                            symbiont: 'video.edit.'+id
                        },
                        success:function(r){
                            $('#sym_video_edit').html(r);
                            $('.sym_video_edit .tabs')
                            .tabs();
                            
                        },
                        dataType: 'html'
                    });
                },
                beforeclose: function(event, ui) {
                    
                },
                close: function(event, ui) {
                    
                },
                buttons:{
                    "Cancel":function(){
                        $(this).dialog("close");
                    },
                    "Ok":function(){
                        t=this;
                        title={};
                        description={};
                        name=$(t).find('.name').val();
                        parent=$(t).find('.parent').val();
                        category_id=$('.sym_video_category_id').text();
                        $(t).find('.title').each(function(){
                            title[$(this).attr('title')]=$(this).val();
                        });
                        $(t).find('.description').each(function(){
                            description[$(this).attr('title')]=$(this).val();
                        });
                        if($('#image').val()&&$('#upvideo').val()){
                            $.ajaxFileUpload({
                                url:'?page=$&upload&name=image&dir=uploads/images/',
                                secureuri:false,
                                fileElementId:'image',
                                dataType: 'json',
                                success: function (data, status){
                                    if(typeof(data.error) != 'undefined'){
                                        if(data.error != ''){
                                            alert(data.error);
                                        }
                                        else{
                                            image=data.name;
                                            $.ajaxFileUpload({
                                                url:'?page=$&upload&name=video&dir=uploads/videos/',
                                                secureuri:false,
                                                fileElementId:'upvideo',
                                                dataType: 'json',
                                                success: function (data, status){
                                                    if(typeof(data.error) != 'undefined'){
                                                        if(data.error != ''){
                                                            alert(data.error);
                                                        }
                                                        else{
                                                            $.ajax({
                                                                data:{
                                                                    'symbiont': 'video.db_edit.'+id,
                                                                    'cover': 'uploads/images/'+image,
                                                                    'video': 'uploads/videos/'+data.name,
                                                                    'title': title,
                                                                    'description': description,
                                                                    'name': name,
                                                                    'parent': parent,
                                                                    'category_id': category_id,
                                                                    'id': id
                                                                },
                                                                success:function(r){
                                                                    $(t).dialog("close");
                                                                    window.location.reload();
                                                                },
                                                                dataType: 'html'
                                                            });
                                                        }
                                                    }
                                                },
                                                error: function (data, status, e){
                                                    alert(e);
                                                }
                                            });
                                        }
                                    }
                                },
                                error: function (data, status, e){
                                    alert(e);
                                }
                            });
                        }
                        else{
                            $.ajax({
                                data:{
                                    'symbiont': 'video.db_edit.'+id,
                                    'cover': '',
                                    'video': '',
                                    'title': title,
                                    'description': description,
                                    'name': name,
                                    'parent': parent,
                                    'category_id': category_id,
                                    'id': id
                                },
                                success:function(r){
                                    $(t).dialog("close");
                                    window.location.reload();
                                },
                                dataType: 'html'
                            });
                        }
                    }
                }
            });
        }
    });
    //Delete
    $('.sym_video_admin .delete').click(function(){
        t=this;
        id=($(t).parent().parent().find('.id').first().text());
        el=$(t).parent().parent();
        if($('#sym_video_delete').length){
            $('#sym_video_delete').dialog('open');
        }
        else{
        $('#windows').append('<div id="sym_video_delete" title="video delete">Are you sure?</div>');
            $('#sym_video_delete').dialog({
                modal: true,
                width: 200,
                open: function(event, ui) {
                },
                buttons:{
                    "Cancel":function(){
                        $(this).dialog("close");
                    },
                    "Ok":function(){
                        $.ajax({
                            data:{
                                'symbiont': 'video.db_delete.'+id
                            },
                            success:function(r){
                                el.remove();
                            },
                            dataType: 'html'
                        });
                        $(this).dialog("close");
                    }
                }
            });
        }
    });
});